package ext.opensource.netty.server.example.http;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.StringTemplateResolver;

public class ThymeleafTest {

	/**
	 * 使用 Thymeleaf 渲染 HTML
	 * 
	 * @param template
	 *            HTML模板
	 * @param params
	 *            参数
	 * @return 渲染后的HTML
	 */
	public static String render(final String template, final Map<String, Object> params) {
		final Context context = new Context();
		context.setVariables(params);
		final TemplateEngine engine = new TemplateEngine();

		final StringTemplateResolver resolver = new StringTemplateResolver();
		engine.setTemplateResolver(resolver);
		return engine.process(template, context);
	}

	public static void testA() {
		final String template = "<p th:text='${title}'></p>";
		final HashMap<String, Object> map = new HashMap<>(16);
		map.put("title", "hello world");
		final String render = ThymeleafTest.render(template, map);
		System.out.println("渲染之后的字符串是:" + render);
	}

	public static void testB() {
		final ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
		//模板所在目录，相对于当前classloader的classpath。
		resolver.setPrefix("static/");

		///模板文件后缀
		// resolver.setSuffix(".html");
		// resolver.setCacheable(false);//设置不缓存
		// resolver.setTemplateMode("HTML5");

		final TemplateEngine engine = new TemplateEngine();
		engine.setTemplateResolver(resolver);
		final Context context = new Context();
		context.setVariable("socketurl", "ws");
		System.out.println(engine.process("websocket.html", context));
	}

	public static void testc() {
		final HttpResourceThymeleaf aa = new HttpResourceThymeleaf();
		aa.setRootDir("static/");
		aa.buildWebSocketRes("websocket.html", null);
	}

	public static void main(final String[] args) throws IOException {
		ThymeleafTest.testA();
		//testB();
		//testc();
	}
}